﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFEdit.Views
{
	internal class PromptValidationRule : ValidationRule
	{
		public TextPrompt.TextValidator Validator { get; set; }
		public PromptValidationRule()
		{
			TextPrompt.Rule = this;
		}
		public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
		{
			return Validator((string)value);
		}
	}
	/// <summary>
	/// Interaction logic for TextPrompt.xaml
	/// </summary>
	public partial class TextPrompt : Window
	{
		internal static PromptValidationRule Rule;
		
		private TextPrompt()
		{
			InitializeComponent();
		}

		private void OKButton_Click(object sender, RoutedEventArgs e)
		{
			if (Validation.GetHasError(this.PromptTextBox)) return;
			this.DialogResult = true;
		}

		public static bool PromptForText(string message, string title, TextValidator validator, out string result)
		{
			TextPrompt prompt = new TextPrompt();
			prompt.PromptLabel.Content = message;
			prompt.Title = title;
			Rule.Validator = validator;

			if (prompt.ShowDialog().GetValueOrDefault())
			{
				result = prompt.PromptTextBox.Text;
				return true;
			}
			else
			{
				result = null;
				return false;
			}
		}

		private static ValidationResult DefaultValidate(string value)
		{
			return new ValidationResult(true, null);
		}

		public delegate ValidationResult TextValidator(string value);
	}
}
